import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Basic demonstration of Exception handling.
 * 
 * @author charlieg
 * @author <a href="mailto:charlieg@cis.udel.edu">charlieg@cis.udel.edu</a>
 * @author <a href="http://www.cis.udel.edu/~charlieg">www.cis.udel.edu/~charlieg</a>
 * @version 1.0, &nbsp; 1 Oct 2009
 *
 */
public class BasicException {
	public static void main(String[] args) {
		/* Some of the methods we call throw exceptions, so we'll
		 * need to surround them with a try/catch statement.
		 */
		try {
			/* Open the file located at the specific path...
			 * be sure to modify to fit your filesystem.
			 */
			FileInputStream input = new FileInputStream("/Users/charlieg/Documents/workspace/ExceptionLab/src/hello.txt");
			
			/* Comment out previous line & uncomment next line
			 * to yield FileNotFoundException.
			 */
			// FileInputStream inputA = new FileInputStream("/bad/path/to/hello.txt");
			
			// create a reader accessing the file we just opened
			BufferedReader reader = new BufferedReader(new InputStreamReader(input));
			
			// uncomment next line to cause IOException: Bad file descriptor
			// input.close();
			
			// use the reader to read a line from the file & print to console
			System.out.println(reader.readLine());
		} catch (IOException e) {
			/* Exception could be IOException, or one that inherits
			 * from it, like FileNotFoundException, etc.
			 * 
			 * We'll often want to actually do something or return
			 * something, but for now we'll just print out the
			 * exception to see what's going on.
			 */
			System.out.println("Could not open file, cause: " + e);
		}
	}
}
